package com.fdq.dianping.config;

import com.fdq.dianping.interceptor.LoginInterceptor;
import com.fdq.dianping.interceptor.TokenRefreshInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.annotation.Resource;

@Configuration
public class MvcConfig implements WebMvcConfigurer {
    //序列化器应在此声明，因为config是被spring管理的，而拦截器没有被spring管理
    @Resource
    private RedisTemplate<String,Object> objectRedisTemplate;



    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //解决跨域
//        registry.addInterceptor(new CorsInterceptor())
//                .addPathPatterns("/**")// 拦截所有请求
//                .order(0);

        //有些页面不需要登录也可以访问
        registry.addInterceptor(new LoginInterceptor()).addPathPatterns("/**").excludePathPatterns("/user/login",
                "/user/senCode",
                "/shop/**",
                "/shop-type/**",
                "/voucher/**",
                "/shop-comment/getByShopId",
                "/user/getUserById",
                "/blog/**",
                "/blog-comments/**",
                "/follow/or/not/**",
                "/file/**").order(2);

        //因为有些页面不需要登录也可以访问，当已登录的用户一直访问不需要登录的页面时可能没有刷新token时间，所以还需要一个专门刷新token的拦截器
        //token刷新拦截器优先，因为登录拦截器需要在threadLocal里取数据，而token刷新拦截器是向threadLocal里写数据的
        registry.addInterceptor(new TokenRefreshInterceptor( objectRedisTemplate)).addPathPatterns("/**")
                .order(1);


    }


}
